package com.tte.common.service.impl;

import com.tte.common.service.ExcelService;
import com.tte.common.utils.office.ExceltUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * excel实现类
 * @Author: DongWH
 * @Date: 2022/5/10 15:01
 */
@Service
public class ExcelServiceImpl implements ExcelService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ExceltUtils exceltUtils;

    @Override
    public List<Map<String,String>> readExcel(String path) throws IOException {
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        try {
            xssfWorkbook = exceltUtils.getXSSFWorkbook(path);
            Sheet sheet = exceltUtils.getSheet(xssfWorkbook, 0);
            for (int i = 0; i < sheet.getLastRowNum(); i++) {
                map.clear();
                Row row = sheet.getRow(i);
                String fondsName = row.getCell(0).getStringCellValue();
                String fondsCode = row.getCell(1).getStringCellValue();
                map.put("fondsName",fondsName);
                map.put("fondsCode",fondsCode);
                list.add(map);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally {
            xssfWorkbook.close();
        }
    }

    @Override
    public boolean write(Object o, String path, int pageSize) {
        return false;
    }

    @Override
    public boolean write(Object o, String path, int pageSize, int pageNum) {
        return false;
    }
}
